package com.kfh.ybracelet.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.bean.GoodsInfo;
import com.kfh.ybracelet.pay.PayActivity;
import com.kfh.ybracelet.sim.NewSimActivity;

import java.util.List;

/**
 * 新开卡 商品item listview
 * Created by Aaron on 16/9/23.
 */
public class PayListAdapter extends BaseAdapter {

    private List<GoodsInfo.NewListEntity> newListEntity;
    private Context ctx;
    private List<GoodsInfo.ProductListEntity> productListEntity;

    private final static int NEW_STYLE = 0x255;
    private final static int PRODUCT_STYLE = 0x256;

    private int currentStyle = 0;//当前的类型

    @Deprecated
    public PayListAdapter(Context ctx, int style, List<GoodsInfo.NewListEntity> newListEntity, List<GoodsInfo.ProductListEntity> productListEntity) {
        this.ctx = ctx;
        this.newListEntity = newListEntity;
        this.productListEntity = productListEntity;
        this.currentStyle = style;
    }
//
//    public void setNewListEntity(List<GoodsInfo.NewListEntity> newListEntity) {
//        this.newListEntity = newListEntity;
//    }

    @Override
    public int getCount() {


        switch (currentStyle) {
            case NEW_STYLE:
                if (newListEntity != null)
                    return newListEntity.size();

                break;
            case PRODUCT_STYLE:

                if (productListEntity != null)
                    return productListEntity.size();
                break;

        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {
            convertView = View.inflate(ctx, R.layout.item_choose_money, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_original = (TextView) convertView.findViewById(R.id.tv_original);
            viewHolder.tv_now = (TextView) convertView.findViewById(R.id.tv_now);
            viewHolder.tv_introduce = (TextView) convertView.findViewById(R.id.tv_introduce);
            viewHolder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        switch (currentStyle) {

            case NEW_STYLE:

                if (initNewInfo(position, convertView, viewHolder)) return convertView;

                break;

            case PRODUCT_STYLE:

                if (initProductInfo(position, convertView, viewHolder)) return convertView;

                break;

        }

        return convertView;
    }

    private boolean initNewInfo(final int position, View convertView, ViewHolder viewHolder) {
        if (newListEntity == null || newListEntity.get(position) == null) {
            return true;
        }

//        DecimalFormat df = new DecimalFormat("######0.00");

        viewHolder.tv_original.setText("原价" + newListEntity.get(position).getPrice() + "元");
//        String salePrice = df.format(newListEntity.get(position).getPrice() - newListEntity.get(position).getPriceOff());
        viewHolder.tv_original.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
//        viewHolder.tv_now.setText("售价:" + salePrice + "元");
        viewHolder.tv_now.setText("售价" + newListEntity.get(position).getPriceOff() + "元");
        viewHolder.tv_introduce.setText(newListEntity.get(position).getSubject());


        if (newListEntity.get(position).isSelected()) {//被选中的状态 设置为蓝色

            setselectedTrue(viewHolder, true, ctx.getResources().getColor(R.color.alipay_color));

        } else {//未被选中的状态 设置为灰色
            setselectedTrue(viewHolder, false, ctx.getResources().getColor(R.color.gray_d));

        }

        //如果只有一个条目默认选中为绿色
        if (newListEntity.size() == 1) {
            setselectedTrue(viewHolder, true, ctx.getResources().getColor(R.color.alipay_color));
            if (ctx instanceof NewSimActivity) {
                NewSimActivity newSimActivity = (NewSimActivity) ctx;
                newSimActivity.productId = newListEntity.get(0).getId();
            }
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ctx instanceof NewSimActivity) {
                    NewSimActivity newSimActivity = (NewSimActivity) ctx;
                    newSimActivity.productId = newListEntity.get(position).getId();
                }

                for (int i = 0; i < newListEntity.size(); i++) {
                    newListEntity.get(i).setSelected(false);
                }

                newListEntity.get(position).setSelected(true);

                notifyDataSetChanged();

            }
        });
        return false;
    }

    private boolean initProductInfo(final int position, View convertView, ViewHolder viewHolder) {
        if (productListEntity == null || productListEntity.get(position) == null) {
            return true;
        }

//        DecimalFormat df = new DecimalFormat("######0.00");

        viewHolder.tv_original.setText("原价" + productListEntity.get(position).getPrice() + "元");
//        String salePrice = df.format(productListEntity.get(position).getPrice() - productListEntity.get(position).getPriceOff());
        viewHolder.tv_original.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
        viewHolder.tv_now.setText("售价:" + productListEntity.get(position).getPriceOff() + "元");
        viewHolder.tv_introduce.setText(productListEntity.get(position).getSubject());

        if (productListEntity.get(position).isSelected()) {//被选中的状态 设置为蓝色

            setselectedTrue(viewHolder, true, ctx.getResources().getColor(R.color.alipay_color));

        } else {//未被选中的状态 设置为灰色
            setselectedTrue(viewHolder, false, ctx.getResources().getColor(R.color.gray_d));

        }

        //如果只有一个条目默认选中为绿色
        if (productListEntity.size() == 1) {
            setselectedTrue(viewHolder, true, ctx.getResources().getColor(R.color.alipay_color));
            if (ctx instanceof PayActivity) {
                PayActivity payActivity = (PayActivity) ctx;
                payActivity.productId = productListEntity.get(0).getId();
            }
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ctx instanceof PayActivity) {
                    PayActivity payActivity = (PayActivity) ctx;
                    payActivity.productId = productListEntity.get(position).getId();
                }

                for (int i = 0; i < productListEntity.size(); i++) {
                    productListEntity.get(i).setSelected(false);
                }

                productListEntity.get(position).setSelected(true);

                notifyDataSetChanged();

            }
        });
        return false;
    }

    private void setselectedTrue(ViewHolder viewHolder, boolean selected, int color) {

        viewHolder.ll.setBackgroundResource(selected ? R.drawable.shape_price_item_true : R.drawable.shape_price_item_false);
        viewHolder.tv_original.setTextColor(color);
        viewHolder.tv_now.setTextColor(color);
        viewHolder.tv_introduce.setTextColor(color);
    }

    private class ViewHolder {

        TextView tv_original;
        LinearLayout ll;
        TextView tv_now;
        TextView tv_introduce;
    }

}
